﻿using BySoft.Server.Business.UserCode;
using BySoft.Server.Common;
using BySoft.Server.Consts;
using BySoft.Web.Controls;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.ComPage
{
    public partial class QueryMMaterialMatReturn : BasePage
    {
        #region 变量定义
        int m_count = 0;

        #endregion

        #region 属性定义
        private DataTable DtMatReturnData
        {
            get
            {
                if (null == Session["DtMatReturnData"])
                {
                    return CreateDT();
                }
                else
                {
                    return (DataTable)Session["DtMatReturnData"];
                }
            }
            set
            {
                Session["DtMatReturnData"] = value;
            }
        }

        private DataTable DtMatReturnDataTemp
        {
            get
            {
                if (null == Session["DtMatReturnDataTemp"])
                {
                    return CreateDT();
                }
                else
                {
                    return (DataTable)Session["DtMatReturnDataTemp"];
                }
            }
            set
            {
                Session["DtMatReturnDataTemp"] = value;
            }
        }

        private DataTable CreateDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("BillDetailID");//0
            dt.Columns.Add("MatInStockDetailID");//1 
            dt.Columns.Add("MatInStockID");//2 
            dt.Columns.Add("WorkOrderNo");//4
            dt.Columns.Add("MaterialID");//5
            dt.Columns.Add("MaterialName");//6
            dt.Columns.Add("Specifications");//7
            dt.Columns.Add("Unit");//8
            dt.Columns.Add("InStockNum");//9
            dt.Columns.Add("LeftNums");//10
            dt.Columns.Add("Nums");//11
            dt.Columns.Add("MMNumber");//13
            dt.Columns.Add("SingleMMNumber");//13
            dt.Columns.Add("IsNew");//14 
            dt.Columns.Add("IsDel");//14
            return dt;
        }
        #endregion

        #region 初始化及绑定控件
        private void BindData(int pageIndex)
        {
            string strFrom = CommonMethod.GetStartTime(cpFrom.Value);
            string strTo = CommonMethod.GetEndTime(cpTo.Value);
            DataTable dtData = BsMatReturnBusiness.SelectMat(this.txtMatInstockID.Text.Trim(), strFrom, strTo, txtMaterialID.Text.Trim(), txtMaterialName.Text.Trim(), hidMatReturnID.Value.Trim(), pageIndex, pcData.PageSize, out m_count);

            pcData.RecordCount = m_count;
            if (dtData.Rows.Count > 0)
            {
                grdResult.DataSource = dtData;
                grdResult.DataBind();
                pcData.CurrentIndex = pageIndex;
                pcData.Visible = true;
                EnableGridViewRow(grdResult, DtMatReturnDataTemp, "MatInStockDetailID", false);
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dtData);
                pcData.Visible = false;
            }
        }

        public void EnableGridViewRow(OneSoft.Web.UI.WebControls.SGridView sgv, DataTable dt, string strCompareField, bool bEnable)
        {
            if (null == dt || dt.Rows.Count < 1)
            {
                return;
            }
            DataTable dtTemp = dt.Copy();
            foreach (DataRow dr in dtTemp.Rows)
            {
                if (CommonMethod.GetString(dr["IsDel"]) == "1")
                {
                    continue;
                }
                foreach (GridViewRow gr in sgv.Rows)
                {
                    Label lblCompareField = gr.FindControl("lbl" + strCompareField) as Label;
                    if (lblCompareField.Text.Trim().Equals(dr[strCompareField].ToString().Trim()))
                    {
                        if (gr.FindControl("cbChoose") != null)
                            ((CheckBox)gr.FindControl("cbChoose")).Checked = true;
                        if (bEnable)
                            gr.Enabled = false;
                        //if (dr["CheckStatus"].ToString().Trim() != "2")
                        //    gr.Enabled = false;
                        break;
                    }
                }
            }
        }

        #endregion

        #region 控件事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hidMatReturnID.Value = UIHelper.GetQueryStringValue("mid");
                DtMatReturnDataTemp = null;
                DtMatReturnDataTemp = DtMatReturnData.Copy();
                grdResult.Style.Add("word-break", "keep-all");
                grdResult.Style.Add("word-wrap", "normal");
                BindData(0);
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AddPro();
            BindData(0);
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            AddPro();
            if (DtMatReturnDataTemp.Rows.Count < 1)
            {
                UIHelper.Alert(this.Page, String.Format(Message.PleaseSelect, "原料信息"));
                return;
            }
            DtMatReturnData = DtMatReturnDataTemp.Copy();
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>setCookie(dialogReturn_key,'success',1);CloseTopDialog();</script>");
        }

        protected void pcData_PageIndexClick(object sender, PageIndexClickEventArgs e)
        {
            AddPro();
            BindData(e.PageIndex);
        }
        #endregion

        #region 本地方法
        private void AddPro()
        {
            DtMatReturnDataTemp = GetAddPro(DtMatReturnDataTemp, "MatInStockDetailID");
        }

        private DataTable GetAddPro(DataTable dt, string strCompareField)
        {
            DataTable dtTemp = dt.Copy();
            foreach (GridViewRow gvr in grdResult.Rows)
            {
                if (gvr.RowType == DataControlRowType.DataRow)
                {
                    Label lblCompareField = gvr.FindControl("lbl" + strCompareField) as Label;

                    if (((CheckBox)gvr.FindControl("cbChoose")).Checked && gvr.Enabled)
                    {
                        if ((dtTemp.Select(string.Format("{0}='{1}' and IsDel<>'1'", strCompareField, lblCompareField.Text))).Length == 0)
                        {

                            Label lblMatInStockID = gvr.FindControl("lblMatInStockID") as Label;
                            Label lblWorkOrderNo = gvr.FindControl("lblWorkOrderNo") as Label;
                            Label lblMaterialID = gvr.FindControl("lblMaterialID") as Label;
                            Label lblMaterialName = gvr.FindControl("lblMaterialName") as Label;
                            Label lblSpecifications = gvr.FindControl("lblSpecifications") as Label;
                            Label lblUnit = gvr.FindControl("lblUnit") as Label;
                            Label lblSingleMMNumber = gvr.FindControl("lblSingleMMNumber") as Label;
                            Label lblInStockNum = gvr.FindControl("lblInStockNum") as Label;
                            Label lblLeftNums = gvr.FindControl("lblLeftNums") as Label;

                            DataRow dr = dtTemp.NewRow();
                            dr["BillDetailID"] = "-1";
                            dr["MatInStockDetailID"] = GetGridViewString(lblCompareField);
                            dr["MatInStockID"] = GetGridViewString(lblMatInStockID);
                            dr["WorkOrderNo"] = GetGridViewString(lblWorkOrderNo);
                            dr["MaterialID"] = GetGridViewString(lblMaterialID);
                            dr["MaterialName"] = GetGridViewString(lblMaterialName);
                            dr["Specifications"] = GetGridViewString(lblSpecifications);
                            dr["Unit"] = GetGridViewString(lblUnit);
                            dr["SingleMMNumber"] = GetGridViewString(lblSingleMMNumber);
                            dr["InStockNum"] = GetGridViewString(lblInStockNum);
                            dr["LeftNums"] = GetGridViewString(lblLeftNums);
                            decimal dLeftNums = CommonMethod.ConvertToDecimal(GetGridViewString(lblLeftNums));
                            decimal dRevNum = 0;
                            dRevNum = dLeftNums;
                            decimal SingleMMNumber = CommonMethod.ConvertToDecimal(GetGridViewString(lblSingleMMNumber));
                            decimal dMMNumber = Math.Round(SingleMMNumber * dRevNum, 2);
                            dr["Nums"] = dRevNum;
                            dr["MMNumber"] = dMMNumber;
                            dr["IsNew"] = "1";
                            dr["IsDel"] = "-1";
                            dtTemp.Rows.Add(dr);
                        }
                    }
                    else
                    {
                        DataRow[] drs = dtTemp.Select(string.Format("{0}='{1}' and IsDel<>'1'", strCompareField, lblCompareField.Text));
                        if (drs.Length > 0)
                        {
                            drs[0]["IsDel"] = "1";
                        }
                    }
                }
            }
            return dtTemp;
        }
        #endregion
    }
}

